package com.yuexun.dhdoctor.module.email.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.email.adapter.DraftEdtFileAdapter;
import com.yuexun.dhdoctor.module.email.bean.MailDataBean;
import com.yuexun.dhdoctor.module.email.bean.MailFileBean;
import com.yuexun.dhdoctor.module.email.bean.MailFileDataBean;
import com.yuexun.dhdoctor.module.email.bean.SendBean;
import com.yuexun.dhdoctor.module.email.model.impl.InBoxDetailModel;
import com.yuexun.dhdoctor.module.email.presenter.impl.InBoxDetailPresenter;
import com.yuexun.dhdoctor.module.email.ui.view.IInBoxView;
import com.yuexun.dhdoctor.util.URLImageGetter;
import com.yuexun.dhdoctor.widget.FixedSwipeListView;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/8/3.
 */
public class Act_DraftDetail extends BaseKJActivity implements IInBoxView {
    @Bind(R.id.draft_title)
    TextView draftTitle;
    @Bind(R.id.draft_sender)
    TextView draftSender;
    @Bind(R.id.draft_geter)
    TextView draftGeter;
    @Bind(R.id.draft_time)
    TextView draftTime;
    @Bind(R.id.draft_content)
    TextView draftContent;
    @Bind(R.id.draft_file_list)
    FixedSwipeListView draftFileList;

    private ArrayList<MailDataBean> mailDataBeans = new ArrayList<>();
    private int positon = -1;
    private ArrayList<MailFileDataBean> mailFileDataBeans = new ArrayList<>();

    private InBoxDetailPresenter presenter;

    private DraftEdtFileAdapter adapter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_draftdetail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        mailDataBeans = (ArrayList<MailDataBean>) getIntent().getExtras().getSerializable("maillist");
        positon = getIntent().getExtras().getInt("position");
        presenter = new InBoxDetailPresenter(new InBoxDetailModel(), this);
        setData();
    }

    private void setData() {
        draftTitle.setText(mailDataBeans.get(positon).getTitle());
        draftSender.setText(mailDataBeans.get(positon).getAddresser());
        draftGeter.setText(mailDataBeans.get(positon).getRecipients());
        draftTime.setText(mailDataBeans.get(positon).getUpdate_DT());
        draftContent.setMovementMethod(LinkMovementMethod.getInstance());
        draftContent.setText(Html.fromHtml(mailDataBeans.get(positon).getContent().replace("＜", "<").replace("＞", ">").replace(AppConfig.TEXTVIEW_WEB, AppConfig.TEXTVIEW_WEB_All), new URLImageGetter(draftContent), null));

        String[] file = mailDataBeans.get(positon).getFileURL().split(";");
        for (int i = 0; i < file.length; i++) {
            Map<String, String> params = new HashMap<>();
            params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
            params.put("fileUuid", file[i]);
            presenter.getFileData(params);
        }
    }

    @Override
    @OnClick({R.id.back, R.id.edt_tv, R.id.inbox_delete, R.id.inbox_back, R.id.inbox_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.edt_tv:
                Bundle bundle = new Bundle();
                Intent intent = new Intent(aty, Act_DraftEdt.class);
                bundle.putSerializable("maillist", mailDataBeans.get(positon));
                bundle.putSerializable("filelist", mailFileDataBeans);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.inbox_delete:
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("mailID", mailDataBeans.get(positon).getMailID());
                presenter.deleteMail(params);
                break;
            case R.id.inbox_back:
                if (positon != 0) {
                    positon--;
                    if (adapter != null && mailFileDataBeans.size() != 0) {
                        mailFileDataBeans.clear();
                        adapter.notifyDataSetChanged();
                    }
                    setData();
                } else {
                    ViewInject.toast("已到顶");
                }
                break;
            case R.id.inbox_next:
                if (positon != (mailDataBeans.size() - 1)) {
                    positon++;
                    if (adapter != null && mailFileDataBeans.size() != 0) {
                        mailFileDataBeans.clear();
                        adapter.notifyDataSetChanged();
                    }
                    setData();
                } else {
                    ViewInject.toast("已到顶");
                }
                break;
        }
    }


    @Override
    public void getFileData(MailFileBean mailFileBean) {
        if (mailFileBean.getStatus().equals("200")) {
            mailFileDataBeans.add(mailFileBean.getDatas().get(0));
            adapter = new DraftEdtFileAdapter(this, mailFileDataBeans);
            draftFileList.setAdapter(adapter);
        } else {
            Log.e("jx", "文件信息获取失败");
        }
    }

    @Override
    public void deleteMail(SendBean sendBean) {
        if (sendBean.getStatus().equals("200")) {
            if (positon == (mailDataBeans.size() - 1) && mailDataBeans.size() != 1) {
                mailDataBeans.remove(positon);
                positon--;
                mailFileDataBeans.clear();
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                setData();
            } else if (positon == 0 && mailDataBeans.size() == 1) {
                ViewInject.toast("已删除全部邮件");
                finish();
            } else {
                mailDataBeans.remove(positon);
                mailFileDataBeans.clear();
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                setData();
            }
        } else {
            ViewInject.toast("删除邮件失败");
        }
    }
}
